<?php
class Odstranenie_personalu_model extends CI_Model {
	
	public function get_jedalnicek()
	{
		$query = $this->db->query('SELECT Jedlo.Nazov_jedla FROM Jedlo');
		return $query->result_array();
	}
	
	public function zobraz_personal()
	{
		$this->db->select('Rodne_cislo, Meno, Priezvisko')
					->from('Zdravotnicky_personal');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function _zakazane_jedla($slug)
	{
		$this->db->select('Obsahuje.Nazov_jedla');
		$this->db->from('Obsahuje');
		$this->db->join('ZakazanaIngrediencia','Obsahuje.Nazov = ZakazanaIngrediencia.Nazov');
		$this->db->join('Pacient','Pacient.Dieta = ZakazanaIngrediencia.Dieta');
		$this->db->where('Pacient.Rodne_cislo', $slug);
		$this->db->distinct();
		
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	public function _vsetky_jedla($slug)
	{
		$this->db->select('ID, Nazov_jedla');
		$this->db->from('Jedlo');
		
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	public function povolene_jedla($slug)
	{
		static $ret = array();
		foreach($this->jedalnicek_model->_vsetky_jedla($slug) as $jedlo) {
			$vypis = true;
			foreach($this->jedalnicek_model->_zakazane_jedla($slug) as $zakazane) {
				if ($jedlo['ID'] == $zakazane['Nazov_jedla'])
					$vypis = false;
			}
			if ($vypis) {
				$vloz = array(
					'Nazov_jedla' => $jedlo['Nazov_jedla'],
					'ID' => $jedlo['ID']
				);
				array_push($ret, $vloz);
			}
		}
		return $ret;
	}
	
	public function zrus_jedlo($slug)
	{
		$this->db->delete('Vyber', array('Rodne_cislo' => $slug));
		
	}
	
	public function vlozit_jedlo($slug)
	{
		if (isset($_POST['objednane_jedlo'])) {
			$query = $this->db->select('ID')
				->where('Nazov_jedla', $_POST['objednane_jedlo'])
				->get('Jedlo');
					
			$ret = $query->row_array();
			
			$data = array(
				'Rodne_cislo' => $slug,
				'Nazov_jedla' => $ret['ID']
			);
			$this->db->insert('Vyber', $data); 	
		}
	}
	
	public function zistit_login($slug)
	{
		$query = $this->db->select('Uzivatelske_meno')->from('Pacient')->where('Rodne_cislo', $slug)->get();
		$row = $query->row_array();
		return $row['Uzivatelske_meno'];
	}
	
	public function objednane_jedlo($slug)
	{
		$query = $this->db->select('Jedlo.Nazov_jedla')
						->from('Jedlo')
						->join('Vyber', 'Jedlo.ID = Vyber.Nazov_jedla')
						->where('Vyber.Rodne_cislo', $slug)->get();
		$row = $query->row_array();
		if (!empty($row))
			return $row['Nazov_jedla'];
		else
			return "(žiadne)";
		
	}
	
	public function get_diagnoza($slug)
		{
		$query = $this->db->select('Diagnoza')->from('Pacient')->where('Rodne_cislo', $slug)->get();
		$row = $query->row_array();
		return $row['Diagnoza'];
	}
		
		
	public function odstranit_pacienta($slug)
	{
		$this->db->delete('Ubytovany', array('Rodne_cislo' => $slug));
		$this->db->delete('Pacient', array('Rodne_cislo' => $slug));
	}
	

	
	public function odstranit_personal($slug)
	{
		$this->db->delete('Zdravotnicky_personal', array('Rodne_cislo' => $slug));
	}
}